//
//  NetworkTools.swift
//  wbxm
//
//  Created by apple on 15/12/21.
//  Copyright © 2015年 apple. All rights reserved.
//

import AFNetworking
enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}
let dataErrorDomain = "com.baidu.data.error"

class NetworkTools: AFHTTPSessionManager {
    //单利对象
    static let sharedTools: NetworkTools = {
        //设置base url
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    func requestJSONDict(method:HTTPMethod,urlString: String , parameters: [String : String]?,finished: (dict: [String : AnyObject]?,error: NSError?) -> ()) {
        
        if method == HTTPMethod.POST {
            //发送POST请求
            POST(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
                    //请求成功的回掉
//                    print(result)
                    guard let dict = result as? [String : AnyObject] else {
                        //不能够转换为字典数据
                        //自定义erro  错误信息
                        //code : 错误码  错误状态  自定义的错误信息 一般用负数
                        //NSLocalizedDescriptionKey  错误描述信息
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        finished(dict: nil, error: myError)
                        
                        print(myError)
                        return
                    }
                    
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    //请求失败的回掉
                    print(error)
                    finished(dict: nil, error: error)
            }

        }else{
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
//                    print(result)
                    guard let dict = result as? [String : AnyObject] else {
                        //不能够转换为字典数据
                        //自定义erro  错误信息
                        //code : 错误码  错误状态  自定义的错误信息 一般用负数
                        //NSLocalizedDescriptionKey  错误描述信息
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        finished(dict: nil, error: myError)
                        
                        print(myError)
                        return
                    }
                    
                    finished(dict: dict, error: nil)
                    
                    
                }) { (_, error) -> Void in
                    print(error)
                    finished(dict: nil, error: error)
            }
        }
    }
    //上传图片的工具方法
    func uploadImage(urlString: String,paramenters:[String : String]?,imageData: NSData,finished: (dict: [String : AnyObject]?,error: NSError?) -> ()) {
        
        
        POST(urlString, parameters: paramenters, constructingBodyWithBlock: { (formData) -> Void in
            //将图片的二进制数据添加到formData中
            //data 需要上床的文件的二进制数据
            //name 服务器接受上传文件的需要对应字段
            // fileName 服务器存储的名称 随便取  新浪微博 会自己去更改名字
            // mimeType 上传的文件的类型
            formData.appendPartWithFileData(imageData, name: "pic", fileName: "xxx", mimeType: "image/jpeg")
            
            }, progress: { (p) -> Void in
                print(p)
            }, success: { (_, result) -> Void in
                print(result)
                //上传成功
                if let dict = result as? [String : AnyObject] {
                    finished(dict: dict, error: nil)
                }
            }) { (_, error) -> Void in
                print(error)
                //上传失败
                finished(dict: nil, error: error)
        }
        
    }
    
    
}







